package cn.zhizihua.controller;


import cn.zhizihua.domain.po.Address;
import cn.zhizihua.domain.po.Food;
import cn.zhizihua.domain.po.User;
import cn.zhizihua.domain.query.AddressQuery;
import cn.zhizihua.domain.query.UserQuery;
import cn.zhizihua.domain.vo.ResultBean;
import cn.zhizihua.exception.AppException;
import cn.zhizihua.exception.AppExceptionCodeMsg;
import cn.zhizihua.service.IAddressService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhizihua
 * @since 2024-12-11
 */
@RestController
@Api(tags = "地址管理接口")
@RequestMapping("/address")
public class AddressController {
    @Autowired
    IAddressService addressService;

    @ApiOperation("地址添加或修改")
    @PostMapping      //  请求方式 + 请求路径   post  /address
    public ResultBean save(@RequestBody Address address){
        System.out.println("----AddressController-----save--------");
        System.out.println(address);
        //添加或修改地址信息
        boolean saveOrUpdate = addressService.saveOrUpdate(address);
        //判断是否添加成功
        if (!saveOrUpdate){
            throw new AppException(AppExceptionCodeMsg.SAVE_OR_UPDATE_FAILE);
        }
        //返回成功
        return ResultBean.SUCCESS();
    }

    //  delete   /address/1003
    @ApiOperation("根据地址id删除地址信息")
    @DeleteMapping("/{id}")   // http://localhost:8080/address/1003
    public ResultBean del(@PathVariable("id") Integer id){
        System.out.println("-------AddressController--------del-------------");
        System.out.println(id);
        //删除地址
        boolean del = addressService.removeById(id);
        if (!del){
            throw new AppException(AppExceptionCodeMsg.DELETE_FAIL);
        }
        return ResultBean.SUCCESS();
    }

    //  get   /address/1004
    @ApiOperation("根据地址id查询地址信息")
    @GetMapping("/{id}")   // http://localhost:8080/address/1003
    public ResultBean getById(@PathVariable("id") Integer id){
        System.out.println("-------AddressController--------getById-------------");
        System.out.println(id);
        //id查询地址信息
        Address byId = addressService.getById(id);
        return ResultBean.SUCCESS(byId);
    }

    @ApiOperation("查询全部地址信息")
    @GetMapping("/all")    //  get  /user/all
    public ResultBean getAll(){
        System.out.println("-------AddressController-----getAll--------");
        //获取全部数据
        List<Address> list = addressService.list();
        return ResultBean.SUCCESS(list);
    }


    @ApiOperation("分页查询地址信息")
    @PostMapping("/page")   // post  /address/page
    public ResultBean getByPage(@RequestBody AddressQuery query){
        System.out.println("-------AddressController--------getByPage-------------");
        System.out.println(query);
        Page<Address> page = new Page<>(query.getPageNow(), query.getPageCount());
        addressService.page(page, query.getQueryWrapper());
        if (page.getTotal() == 0){
            throw new AppException(AppExceptionCodeMsg.GET_BY_PAGE_FAILE);
        }
        return ResultBean.SUCCESS(page);
    }


    @ApiOperation("批量删除地址信息")
    @PostMapping("/batch")   // post + /xxx/batch
    public ResultBean batchDel(@RequestBody List<Integer> ids){
        System.out.println("----------AddressController---------batchDel---------------");
        boolean b = addressService.removeBatchByIds(ids);
        if (!b){
            throw new AppException(AppExceptionCodeMsg.BATCH_DEL_FAILE);
        }
        return ResultBean.SUCCESS();
    }


    @ApiOperation("根据用户id查询所属地址信息")
    @GetMapping("/user/{uid}")
    public ResultBean getByBusinessId(@PathVariable("uid") Integer uid){
        System.out.println("----------FoodController---------getByBusinessId---------------");
        if (uid==null || uid==0){
            throw new AppException(AppExceptionCodeMsg.PARAMS_EMPTY);
        }
        List<Address> addressList = addressService.getByUserId(uid);
        return ResultBean.SUCCESS(addressList);
    }
}
